#include "CVSView.h"
#include "CGame.h"
#include "CDevice.h"

#if CONFIG_PLATFORM == PLATFORM_WIN32_VS

namespace GameTutor
{
	CVSView* CVSView::s_pInstace = 0;

	//------------------------------
	// Constructor
	//------------------------------
	CVSView::CVSView(__INT32 w, __INT32 h, bool fullscreen /* = false */, const char* name /* = 0 */)
		: m_iHeight(h), m_iWidth(w), m_isFullScreen(fullscreen),
		  m_hGLRC(0), m_hwndcWindow(0), m_hGameWndc(0)
	{
		s_pInstace = this;
		if (name)
		{
			m_strTitle = new char[strlen(name) + 1];
			strcpy(m_strTitle, name);
		}
		else
		{
			char* xname = "Untitle";
			m_strTitle = new char[strlen(xname) + 1];
			strcpy(m_strTitle, xname);
		}
		InitClientWindow();
		CDevice::GetInstance()->SleepEx(3000); // avoid lag when lauch game
	}

	//------------------------------
	// Destructor
	//------------------------------

	CVSView::~CVSView()
	{
		Destroy();
		delete m_strTitle;
	}

	void CVSView::InitClientWindow()
	{
		// get current instant
		HINSTANCE hInstance = GetModuleHandle(0);
		//-------------------------
		// int WNDCCLASSEX
		WNDCLASSEX wcex;
		wcex.cbSize = sizeof(WNDCLASSEX); 
		wcex.style = CS_HREDRAW | CS_VREDRAW; 
		wcex.lpfnWndProc = WndProc; 
		wcex.cbClsExtra = 0; 
		wcex.cbWndExtra = 0; 
		wcex.hInstance = hInstance;
		wcex.hIcon = 0;//LoadIcon(hInstance, IDI_APPLICATION); 
		wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 
		wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
		wcex.lpszMenuName = 0; 
		wcex.lpszClassName = "main"; 
		wcex.hIconSm = 0;//LoadIcon(wcex.hInstance, IDI_APPLICATION); 
		if (! RegisterClassEx( &wcex ) ) 
		{ 
			return; // ERR, SO QUIT 
		} 
		//---------------------------------- 
		// init HWND 
		DWORD style = WS_POPUP; 
		if(!m_isFullScreen) 
		{ 
			style = WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS; 
		} 
		RECT clientSize; 
		clientSize.top = 0; 
		clientSize.left = 0; 
		clientSize.right = m_iWidth; 
		clientSize.bottom = m_iHeight; 
		AdjustWindowRect(&clientSize, style, FALSE); 
		const int realWidth = clientSize.right - clientSize.left; 
		const int realHeight = clientSize.bottom - clientSize.top; 
		int windowLeft = (GetSystemMetrics(SM_CXSCREEN) - realWidth) / 2; 
		int windowTop = (GetSystemMetrics(SM_CYSCREEN) - realHeight) / 2;

		if (!m_isFullScreen)
		{
			m_hwndcWindow = CreateWindow("main", m_strTitle, style, windowLeft, windowTop, realWidth, realHeight, 0, 0, 0, 0);
		}
		else
		{
			m_hwndcWindow = CreateWindow("main", m_strTitle, style, 0, 0, realWidth, realHeight, 0, 0, 0, 0);
		}

		m_hGameWndc = GetDC(m_hwndcWindow);

		ShowWindow(m_hwndcWindow, SW_SHOW);
		UpdateWindow(m_hwndcWindow);

		if(!m_isFullScreen) 
		{ 
			MoveWindow(m_hwndcWindow, windowLeft, windowTop, realWidth, realHeight, TRUE); 
		} 
		SetActiveWindow(m_hwndcWindow); 
		SetForegroundWindow(m_hwndcWindow); 
		if(m_isFullScreen) 
		{ 
			DEVMODE dm; 
			memset(&dm, 0, sizeof(dm)); 
			dm.dmSize = sizeof(DEVMODE); 
			dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL; 
			dm.dmPelsWidth = m_iWidth; 
			dm.dmPelsHeight = m_iHeight; 
			dm.dmBitsPerPel = 32; 
			ChangeDisplaySettings(&dm, CDS_FULLSCREEN); 
		}
	}

	void CVSView::Update()
	{
		MSG msg;
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			if (msg.hwnd == m_hwndcWindow)
			{
				DispatchMessage(&msg);
			}
		}
	}

	void CVSView::Destroy()
	{
		if (m_isFullScreen)
		{
			ChangeDisplaySettings(NULL, 0);
		}
	}

	LRESULT CALLBACK CVSView::WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
	{
		switch (message)
		{
		case WM_CREATE:
			return 0;
		case WM_PAINT:
			PAINTSTRUCT ps;
			BeginPaint(hwnd, &ps);
			EndPaint(hwnd, &ps);
			return 0;
		case WM_DESTROY:
			CGame::GetInstance()->Exit();
			PostQuitMessage(0);
			return 0;
		}
		return DefWindowProc(hwnd, message, wparam, lparam);
	}
}
#endif